package com.zhongxiangspace.controller.tools;

import com.zhongxiangspace.tools.CaptchaUtil;
import com.zhongxiangspace.tools.FileUtil;
import com.zhongxiangspace.tools.ReadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/util")
public class UtilController {
    @Autowired
    private ReadProperties readProperties;

    /**
     * 登录验证码
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/adminLoginnCaptcha.do", method = { RequestMethod.GET, RequestMethod.POST })
    public void captcha(HttpServletRequest request, HttpServletResponse response)  {
        CaptchaUtil.outputCaptcha(request, response);
       String captcha= (String) request.getSession().getAttribute("captcha");
        System.out.println(captcha);

    }

    @PostMapping(value = "/article/uploaderImg.do",produces = "application/json")
    @ResponseBody
    public Object uploaderImg(MultipartFile imgFile,String dir) throws IOException {
        Map<String, Object> map = new HashMap<>();
        if (!imgFile.isEmpty() && imgFile.getContentType().contains("image/")) {
            FileInputStream in = (FileInputStream) imgFile.getInputStream();
            BufferedImage srcImage = javax.imageio.ImageIO.read(in);
            Map<String, Object>  imagePathMap = FileUtil.saveFile(imgFile, "image/aaa", this.readProperties.getImagePath());
            map.put("url", imagePathMap.get("originalPath"));
            map.put("error", 0);
        }else{
            map.put("message", "错误信息");
            map.put("error", 1);
        }
        return map;
    }

}
